package com.bbs.controller;

import com.bbs.config.CookieConfig;
import com.bbs.dto.LoginFormDTO;
import com.bbs.dto.Result;
import com.bbs.dto.UserDTO;
import com.bbs.dto.UserQueryDTO;
import com.bbs.service.UserService;
import com.bbs.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/users")
@Tag(name = "用户管理", description = "用户相关的接口")
public class UserController {
    
    @Autowired
    private UserService userService;

    @PostMapping("/code")
    @Operation(summary = "发送验证码", description = "发送验证码")
    @Tag(name = "登录注册", description = "登录注册相关接口")
    public Result sendCode(String phone){
        return userService.sendCode(phone);
    }

    @PostMapping("/login")
    @Operation(summary = "登录接口", description = "根据手机号和验证码")
    @Tag(name = "登录注册", description = "登录注册相关接口")
    public Result login(@RequestBody LoginFormDTO loginFormDTO){

        return userService.loginByCode(loginFormDTO);
    }

    @PostMapping
    @Operation(summary = "创建用户", description = "创建新用户")
    @Tag(name = "用户管理", description = "用户相关的接口")
    public Result createUser(@RequestBody UserDTO userDTO) {
        UserVO userVO = userService.createUser(userDTO);
        return Result.ok(userVO);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户信息")
    @Parameter(name = "id", description = "用户ID", required = true)
    public Result getUserById(@PathVariable Long id) {
        UserVO userVO = userService.getUserById(id);
        return Result.ok(userVO);
    }
    
    @GetMapping
    @Operation(summary = "获取所有用户", description = "获取系统中所有用户的信息")
    public Result getAllUsers() {
        return Result.ok(userService.getAllUsers());
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新用户信息", description = "根据用户ID更新用户信息")
    @Parameter(name = "id", description = "用户ID", required = true)
    public Result updateUser(@PathVariable Long id, @RequestBody UserDTO userDTO) {
        UserVO userVO = userService.updateUser(id, userDTO);
        return Result.ok(userVO);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "根据用户ID删除用户")
    @Parameter(name = "id", description = "用户ID", required = true)
    public Result deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.ok();
    }
    
    @Operation(summary = "根据用户名查询", description = "根据用户名查询用户信息")
    @GetMapping("/username/{username}")
    public UserVO getUserByUsername(@Parameter(description = "用户名") @PathVariable String username) {
        return userService.getUserByUsername(username);
    }

    @Operation(summary = "复杂条件查询用户",description = "复杂条件查询用户")
    @PostMapping("/query/list")
    public Result queryUsers(@RequestBody UserQueryDTO userQueryDTO){
 return userService.queryUsers(userQueryDTO);
    }

    @PostMapping("/avatar")
    @Operation(summary = "上传头像", description = "上传用户头像")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file) {
        // TODO: 实现头像上传逻辑
        // 1. 验证文件类型
        // 2. 生成文件名
        // 3. 保存文件
        // 4. 更新用户头像路径
        // 5. 返回头像访问路径
        return null;
    }

    @GetMapping("/avatar")
    @Operation(summary = "获取头像", description = "获取用户头像")
    public Result getAvatar() {
        // TODO: 实现获取当前用户头像的逻辑
        // 1. 获取当前用户
        // 2. 返回头像路径
        return null;
    }

    @DeleteMapping("/avatar")
    @Operation(summary = "删除头像", description = "删除用户头像")
    public Result deleteAvatar() {
        // TODO: 实现删除用户头像的逻辑
        // 1. 获取当前用户
        // 2. 删除头像文件
        // 3. 清空用户头像路径
        return null;
    }
    @PostMapping("/sign")
    public Result sign(){
        return userService.sign();
    }
    @GetMapping("/sign/count")
    public Result signCount(){
        Integer count= userService.signCount();
        return Result.ok(count);
    }
@PostMapping("/cookie/test")
    public void loginSuccess(HttpServletResponse response,String userInfo){
Cookie cookie =new Cookie(CookieConfig.CookieName(),userInfo);
cookie.setMaxAge(CookieConfig.getInterval());
cookie.setHttpOnly(true);
//添加到响应中
       response.addCookie(cookie);
    }

} 